package com.example.service;

import com.example.entity.MbtiQuestion;
import com.example.entity.Results;
import com.example.entity.Student;
import com.example.mapper.MbtiQuestionMapper;
import com.example.mapper.ResultMapper;
import com.example.mapper.StudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MbtiQuestionService {

    @Resource
    private MbtiQuestionMapper mbtiQuestionMapper;
    @Resource
    private ResultMapper resultMapper;
    @Resource
    private StudentMapper studentMapper;


    public List<MbtiQuestion> getAllQuestions() {
        return mbtiQuestionMapper.findAll();
    }

    // 分页查询
    public PageInfo<MbtiQuestion> selectPage(MbtiQuestion mbtiQuestion, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MbtiQuestion> list = mbtiQuestionMapper.selectAll(mbtiQuestion);
        return PageInfo.of(list);
    }

    public void add(MbtiQuestion mbtiQuestion) {
        mbtiQuestionMapper.insert(mbtiQuestion);
    }

    public void update(MbtiQuestion mbtiQuestion) {
        mbtiQuestionMapper.update(mbtiQuestion);
    }

    public void delete(Integer id) {
        mbtiQuestionMapper.delete(id);
    }

    public Map<String, Object> submit(Question question) {
        Map<String, Object> mapa = new HashMap<>();
        Map<String, Integer> mapt = new HashMap<>();
        mapt.put("E", 0);
        mapt.put("I", 0);
        mapt.put("S", 0);
        mapt.put("N", 0);
        mapt.put("T", 0);
        mapt.put("F", 0);
        mapt.put("J", 0);
        mapt.put("P", 0);

        List<Integer> questionId = question.getAnswers().stream()
                .map(i -> i.getQuestionId()).collect(Collectors.toList());
        Map<Integer, String> maps = new HashMap<>();
        for (Question.QuestionList questionList : question.getAnswers()) {
            maps.put(questionList.getQuestionId(), questionList.getChoice());
        }
        List<MbtiQuestion> list = mbtiQuestionMapper.selectIds(questionId);
        for (MbtiQuestion mbtiQuestion : list) {
            String string = maps.get(mbtiQuestion.getQuestionId());
            if (string.equals("A")) {
                Integer integer = mapt.get(mbtiQuestion.getTypeA());
                integer++;
                mapt.put(mbtiQuestion.getTypeA(), integer);
            } else if (string.equals("B")) {
                Integer integer = mapt.get(mbtiQuestion.getTypeB());
                integer++;
                mapt.put(mbtiQuestion.getTypeB(), integer);
            }
        }

        String jsonKey = "";
        Integer E = mapt.get("E");
        Integer I = mapt.get("I");
        if (E >= I) {
            jsonKey += "E";
        } else {
            jsonKey += "I";
        }
        Integer S = mapt.get("S");
        Integer N = mapt.get("N");
        if (S >= N) {
            jsonKey += "S";
        } else {
            jsonKey += "N";
        }
        Integer T = mapt.get("T");
        Integer F = mapt.get("F");
        if (T >= F) {
            jsonKey += "T";
        } else {
            jsonKey += "F";
        }
        Integer J = mapt.get("J");
        Integer P = mapt.get("P");
        if (J >= P) {
            jsonKey += "J";
        } else {
            jsonKey += "P";
        }
        mapa.put("result", jsonKey);

        Student student = studentMapper.selectById(question.getUserId());
        Results results = new Results();
        results.setResult(jsonKey);
        results.setStudentId(student.getStudentId());
        results.setStudentName(student.getStudentName());
        results.setStudentClass(student.getStudentClass());
        results.setStudentMajor(student.getStudentMajor());
        results.setStudentNumber(student.getStudentNumber());
        results.setStudentSchool(student.getStudentSchool());
        resultMapper.insert(results);
        return mapa;
    }
}
  